# -*- coding: utf-8 -*-
import scrapy
from ..items import QunaerItem


class QunaerSpiderSpider(scrapy.Spider):
    name = 'qunaer_spider'
    # allowed_domains = ['aaa']
    start_urls = ['https://piao.qunar.com/']
    base_url = "https://piao.qunar.com"

    def parse(self, response):
        city_list = response.xpath("//ul[@mp-role='hotCityList']/li/a/text()").extract()
        link_list = response.xpath("//ul[@mp-role='hotCityList']/li/a/@href").extract()
        for city, link in zip(city_list, link_list):
            url = self.base_url + link
            yield scrapy.Request(url, callback=self.parse_detail, meta={"city":city})

    def parse_detail(self, response):
        node_list = response.xpath("//div[@class='result_list']/div")

        for node in node_list:

            item = QunaerItem()
            item["city"] = response.meta["city"]
            item["name"] = node.xpath(".//div[@class='show loading']/a/@title").extract_first()
            item["position"] = node.xpath(".//p[@class='address color999']/span/@title").extract_first()
            item["price"] = eval(node.xpath(".//span[@class='sight_item_price']//em/text()").extract_first())
            item["count"] = eval(node.xpath(".//td[@class='sight_item_sold-num']//span/text()").extract_first())
            rank = node.xpath(".//span[@class='level']/text()").extract_first()
            if rank:
                item["rank"] = rank

            yield item
        next_url = self.base_url + str(response.xpath("//div[@class='pager']//a[@class='next']/@href").extract_first())
        print(next_url, '======================================')
        if "10" in next_url:
            return
        yield scrapy.Request(next_url, callback=self.parse_detail, meta={"city": response.meta["city"]})
